﻿Imports TransportadoraToledoDAL
Public Class VeiculosBLL
    Dim objDAL As VeiculosDAL
    Public nomeCombustivel As String

#Region "propriedades"
    Private lCombustiveis As New List(Of CombustiveisVeiculoBLL)

    Public Property plCombustiveis() As List(Of CombustiveisVeiculoBLL)
        Get
            Return lCombustiveis
        End Get
        Set(ByVal value As List(Of CombustiveisVeiculoBLL))
            lCombustiveis = value
        End Set
    End Property

    Private codVeiculo As Integer
    Public Property pcodVeiculo() As Integer
        Get
            Return Me.codVeiculo
        End Get
        Set(ByVal value As Integer)
            Me.codVeiculo = value
        End Set
    End Property

    Private codModelo As Integer
    Public Property pcodModelo() As Integer
        Get
            Return Me.codModelo
        End Get
        Set(ByVal value As Integer)
            Me.codModelo = value
        End Set
    End Property
    Private codMarca As Integer
    Public Property pcodMarca() As Integer
        Get
            Return Me.codMarca
        End Get
        Set(ByVal value As Integer)
            Me.codMarca = value
        End Set
    End Property



    Private placa As String
    Public Property pPlaca() As String
        Get
            Return Me.placa
        End Get
        Set(ByVal value As String)
            Me.placa = value
        End Set
    End Property

    Private chassi As String
    Public Property pChassi() As String
        Get
            Return Me.chassi
        End Get
        Set(ByVal value As String)
            Me.chassi = value
        End Set
    End Property

    Private cor As String
    Public Property pCor() As String
        Get
            Return Me.cor
        End Get
        Set(ByVal value As String)
            Me.cor = value
        End Set
    End Property

    Private categoria As String
    Public Property pCategoria() As String
        Get
            Return Me.categoria
        End Get
        Set(ByVal value As String)
            Me.categoria = value
        End Set
    End Property

    Private capacidade As Integer
    Public Property pCapacidade() As Integer
        Get
            Return Me.capacidade
        End Get
        Set(ByVal value As Integer)
            Me.capacidade = value
        End Set
    End Property

    Private ano_fabricacao As Integer
    Public Property pAno_fabricacao() As Integer
        Get
            Return Me.ano_fabricacao
        End Get
        Set(ByVal value As Integer)
            Me.ano_fabricacao = value
        End Set
    End Property

    Private ano_modelo As Integer
    Public Property pAno_modelo() As Integer
        Get
            Return Me.ano_modelo
        End Get
        Set(ByVal value As Integer)
            Me.ano_modelo = value
        End Set
    End Property

    Private ativo_sn As Char
    Public Property pAtivo_sn() As Char
        Get
            Return Me.ativo_sn
        End Get
        Set(ByVal value As Char)
            Me.ativo_sn = value
        End Set
    End Property

    Private data_cadastro As Date
    Public Property pData_cadastro() As Date
        Get
            Return Me.data_cadastro
        End Get
        Set(ByVal value As Date)
            Me.data_cadastro = value
        End Set
    End Property
#End Region

#Region "Metodos"

    Sub New()

    End Sub

    Sub New(ByVal codveiculo As Integer, ByVal codModelo As Integer, ByVal codMarca As Integer,
            ByVal placa As String, ByVal chassi As String, ByVal cor As String,
            ByVal categoria As String, ByVal capacidade As Integer, ByVal ano_fabricacao As Integer,
            ByVal ano_modelo As Integer, ByVal ativo_sn As Char, ByVal data_cadastro As Date)
        Me.codVeiculo = codveiculo
        Me.codModelo = codModelo
        Me.codMarca = codMarca
        Me.placa = placa
        Me.chassi = chassi
        Me.cor = cor
        Me.categoria = categoria
        Me.capacidade = capacidade
        Me.ano_fabricacao = ano_fabricacao
        Me.ano_modelo = ano_modelo
        Me.ativo_sn = ativo_sn
        Me.data_cadastro = data_cadastro
    End Sub


    Sub limpar()
        Me.codVeiculo = 0
        Me.codModelo = 0
        Me.placa = ""
        Me.cor = ""
        Me.categoria = ""
        Me.chassi = ""
        Me.capacidade = 0
        Me.ano_fabricacao = 0
        Me.ano_modelo = 0
        Me.ativo_sn = ""
        Me.data_cadastro = Nothing
    End Sub

    Sub inserir()
        Try
            Dim objDAL As New VeiculosDAL
            Dim tab As New DataTable
            tab = objDAL.localizar("vei_placa", placa)
            If (tab.Rows.Count = 0) Then
                If (codModelo = 0 Or placa = "" Or cor = "" Or categoria = "" Or capacidade = 0 Or chassi = "" Or ano_fabricacao = 0 Or ano_modelo = 0 Or ativo_sn = "" Or data_cadastro = Nothing) Then
                    Throw New Exception("Preencha todos os campos!")
                Else
                    'inserir
                    objDAL = New VeiculosDAL

                    Me.codVeiculo = objDAL.inserir(codModelo, placa, chassi, cor, categoria, capacidade, ano_fabricacao, ano_modelo, ativo_sn, data_cadastro)
                    cadastracombustiveis()
                End If
                objDAL = Nothing
            Else
                Throw New Exception("Numeração de Placa já esta sendo utilizada por outro Veiculo")
            End If
            
        Catch ex As Exception
            Throw ex
        End Try
        
    End Sub

    Sub alterar()
        Dim objDAL As New VeiculosDAL
        Try
            objDAL.alterar(codVeiculo, codModelo, placa, chassi, cor, categoria, capacidade, ano_fabricacao, ano_modelo, ativo_sn, data_cadastro)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Dim objDAL As New VeiculosDAL
        Try
            objDAL.excluir(codVeiculo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New VeiculosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("vei_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("vei_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New VeiculosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("vei_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("vei_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnterior(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New VeiculosDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("vei_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    Function localizar(ByVal campo As String, ByVal descricao As String)
        Dim tab As DataTable
        Dim objDAL = New VeiculosDAL
        Try
            tab = objDAL.localizar(campo, descricao)
            Me.pcodVeiculo = tab.Rows(0)(0).ToString()
            Me.pPlaca = tab.Rows(0)(3).ToString()
            objDAL = Nothing
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Dim objModelos As New ModelosBLL
        Try
            objDAL = New VeiculosDAL

            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codVeiculo = tab.Rows(0)("vei_codigo").ToString
                Me.codModelo = tab.Rows(0)("mod_codigo").ToString
                Me.placa = tab.Rows(0)("vei_placa").ToString
                Me.cor = tab.Rows(0)("vei_cor").ToString
                Me.categoria = tab.Rows(0)("vei_categoria").ToString
                Me.chassi = tab.Rows(0)("vei_chassi").ToString
                Me.capacidade = tab.Rows(0)("vei_capacidade").ToString
                Me.ano_fabricacao = tab.Rows(0)("vei_ano_fabricacao").ToString
                Me.ano_modelo = tab.Rows(0)("vei_ano_modelo").ToString
                Me.ativo_sn = tab.Rows(0)("vei_ativo_sn").ToString
                Me.data_cadastro = tab.Rows(0)("vei_data_cadastro").ToString
                'limpar()
            End If
            objModelos.localizaMarca(codModelo)
            Me.codMarca = objModelos.pMar_codigo

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Function localizar(ByVal descricao As String)
        Dim tab As DataTable
        Dim objDAL = New VeiculosDAL
        Try
            tab = objDAL.localizar(descricao)
            objDAL = Nothing
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function localizaCombustivel(ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New VeiculosDAL
            tab = objDAL.localizaCombustivel(codigo)
            If (tab.Rows.Count > 0) Then
                nomeCombustivel = tab.Rows(0)("com_tipo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function


    Public Sub localizaModelo(ByVal codigoModelo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New VeiculosDAL
            tab = objDAL.localizaModelo(codigoModelo)
            If (tab.Rows.Count > 0) Then
                Me.codModelo = tab.Rows(0)("mod_codigo").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub


    Function veiculosdisponiveis(ByVal data As Date) As DataTable
        Dim objDAL As New VeiculosDAL
        Dim tab As New DataTable
        Try
            tab = objDAL.veiculosdisponiveis(data)
            If (tab.Rows.Count > 0) Then
                Me.codVeiculo = tab.Rows(0)("vei_codigo").ToString
                Me.codModelo = tab.Rows(0)("mod_codigo").ToString
                Me.placa = tab.Rows(0)("vei_placa").ToString
                Me.cor = tab.Rows(0)("vei_cor").ToString
                Me.categoria = tab.Rows(0)("vei_categoria").ToString
                Me.chassi = tab.Rows(0)("vei_chassi").ToString
                Me.capacidade = tab.Rows(0)("vei_capacidade").ToString
                Me.ano_fabricacao = tab.Rows(0)("vei_ano_fabricacao").ToString
                Me.ano_modelo = tab.Rows(0)("vei_ano_modelo").ToString
                Me.ativo_sn = tab.Rows(0)("vei_ativo_sn").ToString
                Me.data_cadastro = tab.Rows(0)("vei_data_cadastro").ToString
            End If
        Catch ex As Exception

        End Try
        Return tab
    End Function

    Function veiculosdisponiveis(ByVal placa As String) As DataTable
        Dim objDAL As New VeiculosDAL
        Dim tab As New DataTable
        Try
            tab = objDAL.veiculosdisponiveis(placa)
            If (tab.Rows.Count > 0) Then
                Me.codVeiculo = tab.Rows(0)("v.vei_codigo").ToString
                Me.codModelo = tab.Rows(0)("v.mod_codigo").ToString
                Me.placa = tab.Rows(0)("v.vei_placa").ToString
                Me.cor = tab.Rows(0)("v.vei_cor").ToString
                Me.categoria = tab.Rows(0)("v.vei_categoria").ToString
                Me.chassi = tab.Rows(0)("v.vei_chassi").ToString
                Me.capacidade = tab.Rows(0)("v.vei_capacidade").ToString
                Me.ano_fabricacao = tab.Rows(0)("v.vei_ano_fabricacao").ToString
                Me.ano_modelo = tab.Rows(0)("v.vei_ano_modelo").ToString
                Me.ativo_sn = tab.Rows(0)("v.vei_ativo_sn").ToString
                Me.data_cadastro = tab.Rows(0)("v.vei_data_cadastro").ToString
            End If
        Catch ex As Exception

        End Try
        Return tab
    End Function

    Sub cadastracombustiveis()
        Dim comb As New CombustivelBLL
        For Each obj As CombustiveisVeiculoBLL In lCombustiveis
            obj.pveicodigo = Me.pcodVeiculo
            obj.incluir()
        Next
    End Sub
#End Region
End Class
